<?php

namespace App\Store\Controllers\Financial\Statements;

use App\Common\Services\Store\Common as StoreCommon;
use App\Http\Controllers\Controller;
use App\Store\Models\Financial\Statements\Cash;
use Illuminate\Http\Request;
use App\Store\Validate\Financial\Statements\Cash as ValidateCash;

/**
 * 现金流量表
 */
class CashController extends Controller
{
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['taxpayer_identification_number', 'company', 'year', 'this_key_1', 'last_key_1', 'this_key_2', 'last_key_2', 'this_key_3', 'last_key_3', 'this_key_4', 'last_key_4', 'this_key_5', 'last_key_5', 'this_key_6', 'last_key_6', 'this_key_7', 'last_key_7', 'this_key_8', 'last_key_8', 'this_key_9', 'last_key_9', 'this_key_10', 'last_key_10', 'this_key_11', 'last_key_11', 'this_key_12', 'last_key_12', 'this_key_13', 'last_key_13', 'this_key_14', 'last_key_14', 'this_key_15', 'last_key_15', 'this_key_16', 'last_key_16', 'this_key_17', 'last_key_17', 'this_key_18', 'last_key_18', 'this_key_19', 'last_key_19', 'this_key_20', 'last_key_20', 'this_key_21', 'last_key_21', 'this_key_22', 'last_key_22', 'this_key_23', 'last_key_23', 'this_key_24', 'last_key_24', 'this_key_25', 'last_key_25']);
        $Validate = new ValidateCash();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        $cash_data = $req_data + [
            'cp_id' => $cp_id,
        ];
        Cash::create($cash_data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 详情，取时间段最新的数据
     */
    public function getone(Request $request)
    {
        $year = $request['year'] ?? 2024;
        $one = Cash::where(['year', $year])->first();
        if ($one) {
            $one = $one->toArray();
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $one]);
    }

}
